/**
 * 
 */
package model.solver;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import controller.xml.XMLBaliseFormat;

/**
 * Class for managing of {@link ImportSolverParameter} parameter.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class ImportSolverParameter extends SolverParameter {

	public static final int	DEFAULT_NB_PARAMETER	= 15;
	public static final int	DEFAULT_CNF_POSITION	= 1;

	private List<String>	params					= new ArrayList<String>();
	private int				defaultcnfposition		= DEFAULT_CNF_POSITION;
	private int				cnfposition				= defaultcnfposition;
	private int				nbMaxParameter			= DEFAULT_NB_PARAMETER;

	/**
	 * Export the data in XML
	 * 
	 * @param file
	 *            {@link BufferedWriter} to write
	 * @throws IOException
	 */
	@Override
	public void exportXML(BufferedWriter file) throws IOException {

		// open
		// file.newLine();
		file.write("<" + XMLBaliseFormat.PARAMETER + " " + XMLBaliseFormat.TYPE_PARAMETER + "=" + "\""
				+ XMLBaliseFormat.TYPE_PARAMETER_SOLVEUR + "\"" + " >");

		// solver param
		super.exportXML(file);

		// solution
		// file.newLine();
		file.write("<" + XMLBaliseFormat.IMPORT_CNF_POSITION + ">");
		file.write(String.valueOf(cnfposition));
		file.write("</" + XMLBaliseFormat.IMPORT_CNF_POSITION + ">");

		// arg
		for (String arg : params)
		{// ecrit un arg
			file.write("<" + XMLBaliseFormat.IMPORT_ARG + ">");
			file.write(arg);
			file.write("</" + XMLBaliseFormat.IMPORT_ARG + ">");
		}

		// close
		// file.newLine();
		file.write("</" + XMLBaliseFormat.PARAMETER + ">");
	}


	/**
	 * Create a {@link ImportSolverParameter} parameter.
	 * 
	 */
	public ImportSolverParameter() {
		for (int i = 0 ; i < nbMaxParameter ; i++)
			params.add(new String());
	}


	/**
	 * Create a {@link ImportSolverParameter} parameter.
	 * 
	 * @param param
	 *            parameters of the solver
	 */
	public ImportSolverParameter(List<String> param) {
		params.addAll(param);
	}


	/**
	 * Create a {@link ImportSolverParameter} parameter.
	 * 
	 * @param param
	 *            the {@link SolverParameter}
	 */
	public ImportSolverParameter(SolverParameter param) {
		timeOut = param.getTimeOut();
	}

	/**
	 * Return a copy of this {@link ImportSolverParameter}.
	 * 
	 * @return a copy of this {@link ImportSolverParameter}.
	 */
	public ImportSolverParameter copy() {
		ImportSolverParameter res = new ImportSolverParameter(params);
		res.setNbMaxParameter(nbMaxParameter);
		res.setTimeOut(timeOut);
		res.setDefaultcnfposition(defaultcnfposition);

		return res;
	}

	/**
	 * Return the parameters.
	 * 
	 * @return the params
	 */
	public List<String> getParams() {
		return params;
	}

	/**
	 * Add parameters.
	 * 
	 * @param params
	 *            the parameters to add
	 */
	public void addParameters(List<String> params) {
		this.params.addAll(params);
	}

	/**
	 * Add parameter.
	 * 
	 * @param param
	 *            the parameter to add
	 */
	public void addParameter(String param) {
		params.add(param);
	}

	/**
	 * Return the default cnf input position in the parameters.
	 * 
	 * @return the defaultcnfposition
	 */
	public int getDefaultcnfposition() {
		return defaultcnfposition;
	}

	/**
	 * Set the default cnf input position in the parameters.
	 * 
	 * @param defaultcnfposition
	 *            the position to set
	 * @return true if the position has been set
	 */
	public boolean setDefaultcnfposition(int defaultcnfposition) {
		if (defaultcnfposition < nbMaxParameter && defaultcnfposition > 0)
		{
			this.defaultcnfposition = defaultcnfposition;
			return true;
		}
		return false;
	}

	/**
	 * Return the nb Max of Parameter.
	 * 
	 * @return the nbMaxParameter
	 */
	public int getNbMaxParameter() {
		return nbMaxParameter;
	}

	/**
	 * Set the nb Max of Parameter.
	 * 
	 * @param nbMaxParameter
	 *            the nbMaxParameter to set
	 */
	public void setNbMaxParameter(int nbMaxParameter) {
		this.nbMaxParameter = nbMaxParameter;
	}

	/**
	 * Return the position of the cnf.
	 * 
	 * @return the cnfposition
	 */
	public int getCnfposition() {
		return cnfposition;
	}

	/**
	 * Set the position of the cnf.
	 * 
	 * @param cnfposition
	 *            the cnfposition to set
	 * @return true if the position has been set
	 */
	public boolean setCnfposition(int cnfposition) {
		if (cnfposition < nbMaxParameter && cnfposition > 0)
		{
			this.cnfposition = cnfposition;
			return true;
		}
		return false;
	}


}
